<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class kst_kasir extends MY_Controller {
	
	function __construct()
	{
		parent::__construct(); 
		
		$this->load->model('kst_kasir_model', 'User');
		$this->secure_session();
	}
	
	function index(){
		$data['title'] = "Setup Kasir";
		viewByTemplate('kst_kasir/index', $data, 'grid');
	}
	
	function getdata()
	{
		echo $this->User->GetJSON();
	}
	
	function create()
	{
		$data['title'] = "Setup Kasir - Create";
		// $data['data'] = NULL;
		// $data['data']['culture_id'] = 'id';
		
		$data['mode'] = "create";

		// $query = $this->db->query("SELECT nama_cabang FROM (SELECT * FROM kst_kasir ORDER BY nama_cabang DESC) WHERE rownum <= 1");
		// $getquery = $query->row_array();

		// if($getquery)
		// 	$MaxID = $getquery['nama_cabang'];
		// else
		// 	$MaxID = "K00000"; 

		// $NextID = (int) substr($MaxID,1,5);

		// $NextID++;
		
		// $data['nama_cabang'] = "K".sprintf("%05s",$NextID);
		
		// $data['readonly_role'] = false;
		// $data['data']['readonly_role'] = false;
		viewByTemplate('kst_kasir/form', $data, 'form');
	}

	function edit()
	{
		$data['title'] = "Setup Kasir - Edit";
		$data['data'] = $this->User->GetSingleView(get('nama_cabang'));
		$data['data']['user_password'] = '';
		
		
		$data['mode'] = "edit";
		
		$data['readonly_role'] = get('readonly_role')=="" ? false :  true;
		$data['data']['readonly_role'] = $data['readonly_role'];
		viewByTemplate('kst_kasir/form', $data, 'form');
	}

	private function validation()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty			= $this->MessageTemplate("ER001");
		$tpl_duplicate		= $this->MessageTemplate("ER002");
		// $tpl_user_passwordlength	= $this->MessageTemplate("ER007");

		// // Validation
		// if (post("nama_cabang") == '') {
		// 	$error->Add("nama_cabang", "Kode Kasir", $tpl_empty);
		// } else if (post("mode") == "create" && $this->User->GetSingle(post("nama_cabang")) != null) {
		// 	$error->Add("nama_cabang", "Kode Kasir", $tpl_duplicate);
		// }
		
		// if (post("mode") == "create" && trim(post('user_password')) == '') {
		// 	$error->Add("user_password", "Password", $tpl_empty);
		// }else if(trim(post('user_password')) != ''){
		// 	if(strlen(post('user_password'))<5 || strlen(post('user_password'))>20){
		// 		$error->Add("user_password", array('5', '20'), $tpl_user_passwordlength);
		// 	}
		// }

		if (post("nama_cabang") == '') {
			$error->Add("nama_cabang", "Nama Cabang", $tpl_empty);
		} else if (post("mode") == "create" && intval($this->get_data("SELECT 1 FROM kst_kasir WHERE nama_cabang= '".@ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim(post("nama_cabang"))))."'")) > 0) {
			$error->Add("nama_cabang", "Nama Cabang", $tpl_duplicate);
		}else if (post("nama_kasir") == '') {
			$error->Add("nama_kasir", "Nama Kasir", $tpl_empty);
		}else if (post("kode_kasir") == '') {
			$error->Add("kode_kasir", "Kode Kasir", $tpl_empty);
		}
		
		/*if (post("department_code") == '') {
			$error->Add("department_code", "User Name", $tpl_empty);
		}*/
		
		return $error;
	}
	
	function ajax_post()
	{
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";

		$error = $this->validation();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{
				// Fill Post Data
				$data	= post_by_key(array('nama_cabang', 'nama_kasir', 'kode_kasir'));
				
				if (post("mode") == "create")
				{
					//$data['user_password']		= encrypt($data['user_password'],$this->config->item('encryption_key'));

					$this->User->Add($data);
					
					$ls_response = "Data Tersimpan";
					//App.audit_trail("CS001", "create/" + (data.user_id ?? ""));

				}
				else if (post("mode") == "edit")
				{
					//if(trim($data['user_password']) == '') unset($data['user_password']); else $data['user_password'] = encrypt($data['user_password'],$this->config->item('encryption_key'));
					
					
					$this->User->Update($data, array('kode_kasir'	=> $data['kode_kasir']));
					
					$ls_response = "Data Tersimpan [Edited]";
					//App.audit_trail("CS002", "edit/" + (data.user_id ?? ""));

				}
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}

	function ajax_delete()
	{
		$ls_result	= "";
		$kode_kasir	= get('kode_kasir');
		
		if ($kode_kasir != "")
		{
			// Delete Single
			$arr_rule	= array('kode_kasir' => $kode_kasir);
			$ls_result = $this->User->DeleteSingle($arr_rule);

		}
		else
		{
			
			$num_row = intval($this->data['num_row']['deleted']);
			
			for ($i = 0; $i < $num_row; $i++)
			{
				$row	= $this->data['deleted'][$i];
				
				$arr_rule	= array('kode_kasir' => $row['kode_kasir']);
				$ls_result .= $this->User->DeleteSingle($arr_rule);
			}
		}
		
		echo $ls_result;
	}
}
?>